package org.vectrics.recruiter.job.event;

import java.util.Iterator;
import java.util.Locale;

import org.apache.ecs.html.Body;
import org.apache.ecs.html.Font;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.email.RecruitEmailHtmlDefault;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobStatus;

abstract public class JobHtmlEmail extends RecruitEmailHtmlDefault {
	private Job job = null;
	private int tableWidth = 600;
	
	public JobHtmlEmail(Locale locale, Job job) {
		super(job.getDomain(), locale);
		this.job = job;
		if (job == null)
			throw new SystemException("Job is null");
		
	}
	
	/**
	 * @return Returns the job.
	 */
	public Job getJob() {
		return job;
	}

	/**
	 * @param job The job to set.
	 */
	public void setJob(Job job) {
		this.job = job;
	}
	
	public void addJobStatusHistoryTable(Job job, Cell mainCell) {
		mainCell.addElement("<br><br><font style=\"font:normal 0.8em Verdana\">"
				+ this.getPhrase("phrase.job.status.history") + "</font><br><br>");
		
		BorderTable table = new BorderTable();
		table.setWidth(tableWidth);
		mainCell.addElement(table);

		Row row = table.addRow();

		Cell cell = row.addCell();
		Font font = createBoldSmallFont();
		font.addElement(this.getPhrase("prompt.job.status"));
		cell.addElement(font);
		
		cell = row.addCell();
		font = this.createBoldSmallFont();
		font.addElement(this.getPhrase("prompt.date.start"));
		cell.addElement(font);

		cell = row.addCell();
		font = this.createBoldSmallFont();
		font.addElement(this.getPhrase("prompt.date.end"));
		cell.addElement(font);
		
		cell = row.addCell();
		font = this.createBoldSmallFont();
		font.addElement(this.getPhrase("prompt.person.who.changed"));
		cell.addElement(font);
		
		Iterator iter = job.getStatusList().iterator();
		while (iter.hasNext()) {
			JobStatus jobStatus = (JobStatus)iter.next();
			row = table.addRow();

			cell = row.addCell();
			font = createSmallFont();
			font.addElement(this.getPhrase(jobStatus.getStatusType().getDescriptionCode()) + "<br><i>" 
					+this.getPhrase(jobStatus.getStatusReasonType().getName())
					+ "</i>");
			cell.addElement(font);
			
			cell = row.addCell();
			font = this.createSmallFont();
			font.addElement(CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(jobStatus.getDateStart()));
			cell.addElement(font);

			cell = row.addCell();
			font = this.createSmallFont();
			if (jobStatus.getDateEnd() != null)
				font.addElement(CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(jobStatus.getDateEnd()));
			cell.addElement(font);
			
			cell = row.addCell();
			font = this.createSmallFont();
			font.addElement(jobStatus.getPerson().getName());
			cell.addElement(font);
			
		}
	}
	
	
	public void addJobInfoTable(Job job, Cell mainCell) {
		mainCell.addElement("<br><br><font style=\"font:normal 0.8em Verdana\">"
				+ this.getPhrase("phrase.job.information") + "</font><br><br>");

		
		BorderTable table = new JobInfoEmailTable(this.getLocale(), job);
		table.setWidth(tableWidth);
		mainCell.addElement(table);
	}
	
	
}
